package com.example.assign2_list;

import java.util.Calendar;
import java.text.DateFormat;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class SetAlarms extends Activity {
	private String defaultAlarm = "25";
	private Intent alarm;
	private SharedPreferences pref;
	private int alarmnr;
	private int minute;
	private int hour;
	private int second = 0;
	private boolean isCancel;
	private String[][] alarms;
	private Button alarmButton1;
    private Button alarmButton2;
    private Button alarmButton3;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_alarms);
        alarmButton1 = (Button)findViewById(R.id.first_larm);
        alarmButton2 = (Button)findViewById(R.id.second_larm);
        alarmButton3 = (Button)findViewById(R.id.third_larm);
        
        alarms = new String[3][3];
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        alarm = new Intent(this, Alarm.class);
        
        /*	Get saved values and check if alarm is set	*/
        for(int i = 0; i < 3; i++){
        	for(int j = 0; j < 3; j++){
        		alarms[i][j] = pref.getString("alarm" + i + "." + j, defaultAlarm);
        	}
        }
    }
	
	public void onResume(){
		super.onResume();
		/* Get saved values	*/
        for(int i = 0; i < 3; i++){
        	for(int j = 0; j < 3; j++){
        		alarms[i][j] = pref.getString("alarm" + i + "." + j, defaultAlarm);
        	}
        }
	}
	
	public void onStop(){
		super.onStop();
		/*	Save time values	*/
		Editor edit = pref.edit();
        for(int i = 0; i < 3; i++){
        	for(int j = 0; j < 3; j++){
        		edit.putString("alarm" + i + "." + j, alarms[i][j]);
        	}
        }
		edit.commit();
	}
	
	/*	onClick Alarm 1	*/
	public void setAlarm1(View v){
		alarm.putExtra("alarm", 0);
		startAlarmActivity();
	}
	
	/*	onClick Alarm 2	*/
	public void setAlarm2(View v){
		alarm.putExtra("alarm", 1);
		startAlarmActivity();
	}
	
	/*	onClick Alarm 3	*/
	public void setAlarm3(View v){
		alarm.putExtra("alarm", 2);
		startAlarmActivity();
	}
	
	/*	Start intent with TimePicker	*/
	public void startAlarmActivity(){
		startActivityForResult(alarm, 1);
	}
	
	/*	Get results from Alarm.class	*/
	@Override
    public void onActivityResult(int requestCode,int resultCode,Intent data)
    {
		super.onActivityResult(requestCode, resultCode, data);
		Calendar calen = Calendar.getInstance();
		
		/*	Get time and alarm	*/
		alarmnr = data.getExtras().getInt("alarmnr", 0);
		hour = data.getExtras().getInt("hour", Integer.parseInt(defaultAlarm));
		minute = data.getExtras().getInt("minute", Integer.parseInt(defaultAlarm));
		second = 0;
		isCancel = data.getExtras().getBoolean("isCancel");
		
		if(alarmnr == 0){
			alarmButton1 = (Button)findViewById(R.id.first_larm);
			
			/*	Set PendingIntent	*/
		    Intent intent = new Intent(this, MessageReceiver.class);
		    intent.putExtra("message", "The alarm has gone off");
		    PendingIntent alarmIntent = PendingIntent.getBroadcast(SetAlarms.this,0, intent, 0);
		
		    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
		    
		    if(isCancel){
		    	am.cancel(alarmIntent);
		    }
		    else{
		    	/* Save values in array	*/
				alarms[alarmnr][0] = "" + hour;
				alarms[alarmnr][1] = "" + minute;
				alarms[alarmnr][2] = "" + second;
				
				/*	Set time in right format	*/
			    calen.set(Calendar.HOUR_OF_DAY, hour);
			    calen.set(Calendar.MINUTE, minute);
			    calen.set(Calendar.SECOND, second);
			    alarmButton1.setText("Alarm 1 - is set");	
			    
			    /*	if clock is set back in time add a day */
			    if(calen.getTimeInMillis() < System.currentTimeMillis()){
			    	calen.add(Calendar.DATE, 1);
				    String date = DateFormat.getDateTimeInstance().format(calen.getTimeInMillis());
				    Toast.makeText(this, "Alarm is set: " + date, Toast.LENGTH_LONG).show();
			    }
			    else{
				    String date = DateFormat.getDateTimeInstance().format(calen.getTimeInMillis());
				    Toast.makeText(this, "Alarm is set: " + date, Toast.LENGTH_LONG).show();
			    }
			    
			    // Schedule the alarm
			    long startTime = calen.getTimeInMillis();
		    	am.set(AlarmManager.RTC_WAKEUP, startTime, alarmIntent);
		    }
		}
		else if(alarmnr == 1){
			alarmButton2 = (Button)findViewById(R.id.second_larm);
		    
		    /*	Set PendingIntent	*/
		    Intent intent = new Intent(this, MessageReceiver1.class);
		    intent.putExtra("message", "The one-shot alarm has gone off");
		    PendingIntent alarmIntent = PendingIntent.getBroadcast(SetAlarms.this,0, intent, 0);
		   
		    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
		    
		    if(isCancel){
		    	am.cancel(alarmIntent);
		    }
		    else{
		    	/* Save values in array	*/
				alarms[alarmnr][0] = "" + hour;
				alarms[alarmnr][1] = "" + minute;
				alarms[alarmnr][2] = "" + second;
				
				/*	Set time in right format	*/
			    calen.set(Calendar.HOUR_OF_DAY, hour);
			    calen.set(Calendar.MINUTE, minute);
			    calen.set(Calendar.SECOND, second);
			    alarmButton2.setText("Alarm 2 - is set");
			    
			    if(calen.getTimeInMillis() < System.currentTimeMillis()){
			    	calen.add(Calendar.DATE, 1);
			    }
			    else{
				    String date = DateFormat.getDateTimeInstance().format(calen.getTimeInMillis());
				    Toast.makeText(this, "Alarm is set: " + date, Toast.LENGTH_LONG).show();
			    }
			    
			    // Schedule the alarm
			    long startTime = calen.getTimeInMillis();
		    	am.set(AlarmManager.RTC_WAKEUP, startTime, alarmIntent);
		    }
		    
		}
		else if(alarmnr == 2){
			alarmButton3 = (Button) findViewById(R.id.third_larm);
		    
		    /*	Set PendingIntent	*/
		    Intent intent = new Intent(this, MessageReceiver2.class);
		    intent.putExtra("message", "The one-shot alarm has gone off");
		    PendingIntent alarmIntent = PendingIntent.getBroadcast(SetAlarms.this,0, intent, 0);
		    
		    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
		    
		    if(isCancel){
		    	am.cancel(alarmIntent);
		    }
		    else{
		    	/* Save values in array	*/
				alarms[alarmnr][0] = "" + hour;
				alarms[alarmnr][1] = "" + minute;
				alarms[alarmnr][2] = "" + second;
				
				/*	Set time in right format	*/
			    calen.set(Calendar.HOUR_OF_DAY, hour);
			    calen.set(Calendar.MINUTE, minute);
			    calen.set(Calendar.SECOND, second);
			    alarmButton3.setText("Alarm 3 - is set");
			    
			    if(calen.getTimeInMillis() < System.currentTimeMillis()){
			    	calen.add(Calendar.DATE, 1);
			    }
			    else if(calen.getTimeInMillis() >= System.currentTimeMillis()){
				    String date = DateFormat.getDateTimeInstance().format(calen.getTimeInMillis());
				    Toast.makeText(this, "Alarm is set: " + date, Toast.LENGTH_LONG).show();
			    }
		    	
			    // Schedule the alarm
			    long startTime = calen.getTimeInMillis();
		    	am.set(AlarmManager.RTC_WAKEUP, startTime, alarmIntent);
		    }
		}
		else{
			System.out.println("Error: Wrong alarm number");
		} 
    }
	
	/*	onClick back-button */
	public void backToParent(View v){
		finish();
	}
}
